package filters

import (
	"gitchina/kly_service/models"
	_ "gitchina/kly_service/util"
	_ "github.com/astaxie/beego"
	"github.com/astaxie/beego/context"
	"net/http"
)

var auth_map = map[string]byte{
	"101002": 1,
	"101003": 1,
	"101007": 1,
	"101008": 1,
	"101009": 1,
	"101010": 1,
	"101011": 1,
	"101012": 1,
	"101014": 1,
	"101015": 1,
	"101016": 1,
	"101017": 1,
	"102003": 1,
	"102004": 1,
	"102005": 1,
	"102006": 1,
	"102007": 1,
	"102012": 1,
	"102013": 1,
	"102015": 1,
	"102016": 1,
	"102017": 1,
	"102018": 1,
	"102019": 1,
	"102020": 1,
	"102021": 1,
	"102022": 1,
	"102023": 1,
	"102026": 1,
	"102029": 1,
	"102030": 1,
	"102031": 1,
	"102032": 1,
	"103001": 1,
	"103002": 1,
	"103003": 1,
	"103005": 1,
	"103006": 1,
	"103007": 1,
	"103008": 1,
	"103009": 1,
	"103010": 1,
	"103011": 1,
	"103012": 1,
	"108001": 1,
	"108002": 1,
	"108003": 1,
	"108004": 1,
}

var AuthFilterFunc = func(ctx *context.Context) {
	id, ok := ctx.Input.Params()[":id"]
	if ok {
		if _, ok := auth_map[id]; ok {
			var auth models.Auth
			token := ctx.Request.Header.Get("Auth-Token")
			if pass, mbrId := auth.Authenticate(token); !pass {
				http.Error(ctx.ResponseWriter, "", http.StatusUnauthorized)
			} else {
				ctx.Input.SetData("mbrId", mbrId)
			}
		}
	}
}
